package com.pgeneral.user.management.service.impl;

import com.pgeneral.user.management.dao.UserSoproleMapper;
import com.pgeneral.user.management.dto.UserSopRoleDTO;
import com.pgeneral.user.management.entity.UserSopRole;
import com.pgeneral.user.management.entity.UserSopScopeData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.pgeneral.user.management.entity.UserSopRoleScope;

import java.util.List;

import com.pgeneral.user.management.dao.UserSoprolescopeMapper;
import com.pgeneral.user.management.service.UserSoprolescopeService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserSoprolescopeServiceImpl implements UserSoprolescopeService {

    @Resource
    private UserSoprolescopeMapper dao;

    @Resource
    private UserSoproleMapper userSoproleDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveRoleScope(UserSopRoleDTO userSopRoleDTO) {
        int s = 0;
        if (userSopRoleDTO.getUserSopRole() != null) {
            s++;
            userSoproleDao.insert(userSopRoleDTO.getUserSopRole());
        }
        if (!userSopRoleDTO.getUserSopRoleScope().isEmpty()) {
            s++;
            dao.batchInsert(userSopRoleDTO.getUserSopRoleScope());
        }
        return s;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRoleScope(UserSopRoleDTO userSopRoleDTO) {
        int result = 0;
        if (userSopRoleDTO.getUserSopRole() != null) {
            String roleId = userSopRoleDTO.getUserSopRole().getRoleId();
            //删除角色和角色权限表
            userSoproleDao.deleteInfoByRoleId(roleId);
            //存储更新的角色和角色权限表
            result = saveRoleScope(userSopRoleDTO);
        }
        return result;
    }
}
